import 'package:json_annotation/json_annotation.dart';

part 'comic_user_model.g.dart';

@JsonSerializable()
class UserData {
  // final String? id; // 原数据可能是int或String
  // final String? key; // 原数据可能是int或String
  final String? avatar;
  final String? nickname;
  // final String? sign;
  final String? account;
  final String? email;
  final bool? isSubscribe;
  // final int? sex; // 可能为null
  // final int? age; // 可能为null
  // final String? birthDay;
  final int? vipStatus;
  final double? progress;
  // final int? editCount;
  // final int? isOnline;
  // final String? tag;
  final double? balance;
  final String? source;
  final String? inviteCode;
  final int? status;

  // final String? countryCode;
  // final int? isSystem;
  // final String? createTime; // 时间戳可能格式多样
  // final double? lng;
  // final double? lon;
  // final String? ip;

  UserData({
    this.isSubscribe,
    // this.id,
    // this.key,
    this.avatar,
    this.nickname,
    // this.sign,
    this.account,
    this.email,
    // this.sex,
    // this.age,
    // this.birthDay,
    this.vipStatus,
    this.progress,
    // this.editCount,
    // this.isOnline,
    // this.tag,
    this.balance,
    this.source,
    this.inviteCode,
    this.status,
    // this.countryCode,
    // this.isSystem,
    // this.createTime,
    // this.lng,
    // this.lon,
    // this.ip,
  });

  factory UserData.fromJson(Map<String, dynamic> json) =>
      _$UserDataFromJson(json);

  Map<String, dynamic> toJson() => _$UserDataToJson(this);

  // 自定义tag字段的JSON转换方法
  static List<String>? _tagFromJson(dynamic json) {
    if (json == null) return null;
    if (json is String) {
      // 如果是空字符串，返回空列表
      if (json.isEmpty) return [];
      // 如果是逗号分隔的字符串，分割成列表
      return json
          .split(',')
          .map((e) => e.trim())
          .where((e) => e.isNotEmpty)
          .toList();
    }
    if (json is List) {
      return json.map((e) => e.toString()).toList();
    }
    return null;
  }

  static dynamic _tagToJson(List<String>? tags) {
    if (tags == null || tags.isEmpty) return "";
    return tags;
  }
}
